import configparser
import os
import datetime

import yaml

from utils.logger import logger


# 获取本地driver的位置
def get_driver_path():
    config = configparser.ConfigParser()
    config.read(os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), 'selenium.ini'))
    return config


driver_path = get_driver_path().get('win_driver', 'chrome_driver')
print(driver_path)


# 获取yaml文件中需要的数据
def get_data_from_yaml(name, field_list):
    """
    :param name: yaml文件名称
    :param field_list: 要取的字段名称
    :return:
    """
    with open(f"../data/{name}.yaml", 'r', encoding='utf-8') as f:
        complete_data = yaml.safe_load(f)
    data_list = []
    for i in field_list:
        data_list.append(complete_data[i])
    return data_list

    # for i in field_list:
    #     data_list.append(list(map(itemgetter(i), data)))
    # return data_list


# create_data = get_data_from_yaml('create_issue', ['issue'])[0]
# print(create_data)

def get_yaml_data(file_path):
    """
    读取 yaml 文件内容
    file_path: 要读取的 yaml 文件的路径
    """
    with open(file_path, encoding="utf-8") as f:
        datas = yaml.safe_load(f)
    return datas


def computing_time(after_days=2):
    # 获取当前时间
    current_time = datetime.datetime.now()
    time1 = current_time.strftime('%Y-%m-%d')
    logger.info(f"当前时间：{time1}", )

    # 在当前时间基础上增加两天，timedelta() 用于表示特定的时间间隔
    new_time = current_time + datetime.timedelta(days=after_days)
    time2 = new_time.strftime('%Y-%m-%d')
    logger.info(f"增加两天后的时间：{time2}", )
    return time1, time2


import re


def get_order_number(input: str):
    # 正则表达式模式匹配以"W"开头的字符串，直到字符串结束
    pattern = r'W\d+'

    # 使用search()查找第一个匹配的结果
    match = re.search(pattern, input)
    result = match.group()  # 如果找到匹配项，则提取该匹配
    return result


def str_get_num(input: str):
    pattern = r'\d+'
    match = re.search(pattern, input)
    return match.group()
